package com.xyh.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xyh.eduservice.entity.EduSubject;
import com.xyh.eduservice.entity.excel.SubjectData;
import com.xyh.eduservice.entity.subject.OneSubject;
import com.xyh.eduservice.entity.subject.TwoSubject;
import com.xyh.eduservice.listener.SubjectExcelListener;
import com.xyh.eduservice.mapper.EduSubjectMapper;
import com.xyh.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-08-07
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {
    @Override
    public List<OneSubject> getAllOneTwoSubject() {
        //1.查询所有一级分类 parent_id = 0
        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id","0");

        List<EduSubject> oneSubjectList = baseMapper.selectList(wrapperOne);
        //2.查询所有二级分类 parent_id != 0
        QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper<>();
        wrapperOne.ne("parent_id","0");

        List<EduSubject> twoSubjectList = baseMapper.selectList(wrapperTwo);

        //创建 list 集合，用于封装存储最终封装的数据
        List<OneSubject> finalSubjectList = new ArrayList<>();

        //3.封装一级分类
        //查询出来所有的一级分类 list 集合遍历，得到每一个一级分类对象，获取每一个一级分类对象值
        //封装要求的 list 集合里面  finalSubjectList
        for (int i = 0; i < oneSubjectList.size(); i++) {
            //得到 oneSubject 每个 eduSubject
            EduSubject eduSubject = oneSubjectList.get(i);

            //把 eduSubject 里面的值获取出来，放到 OneSubject 对象那个里面
            //多个 OneSubject 放到 finalSubjectList 里面
            OneSubject oneSubject = new OneSubject();
//            oneSubject.setId(eduSubject.getId());
//            oneSubject.setTitle(eduSubject.getTitle());
            BeanUtils.copyProperties(eduSubject,oneSubject);

            finalSubjectList.add(oneSubject);

            //在一级分类循环遍历查询所有的二级分类
            //创建 list 集合封装每个一级分类的二级分类
            List<TwoSubject> twoFinalSubjectList = new ArrayList<>();
            for (int m = 0; m < twoSubjectList.size(); m++) {
                //获取每个二级分类
                EduSubject tSubject = twoSubjectList.get(m);

                if (tSubject.getParentId().equals(eduSubject.getId())){
                    //把 twoSubject 的值复制到 TwoSubject 里面，放到 twoFinalSubjectList 里面
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(tSubject,twoSubject);
                    twoFinalSubjectList.add(twoSubject);
                }
            }

            //把一级下面所有的二级分类放到一级分类里面
            oneSubject.setChildren(twoFinalSubjectList);
        }

        return finalSubjectList;
    }

    @Override
    public void saveSubject(MultipartFile file,EduSubjectService eduSubjectService) {
        //文件输入流
        try {
            InputStream inputStream = file.getInputStream();
            EasyExcel.read(inputStream, SubjectData.class,new SubjectExcelListener(eduSubjectService)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
